package com.gzs.learn.admin.web.controller.serial;

import com.gzs.learn.admin.clients.ISerialClient;
import com.gzs.learn.admin.clients.ISerialQueryClient;
import com.gzs.learn.admin.common.core.controller.BaseController;
import com.gzs.learn.admin.common.core.page.TableDataInfo;
import com.gzs.learn.common.AjaxResult;
import com.gzs.learn.inf.BaseResponseDto;
import com.gzs.learn.inf.PageResponseDto;
import com.gzs.learn.serial.inf.dto.SegmentSearchDto;
import com.gzs.learn.serial.inf.dto.SerialAllocDto;
import com.gzs.learn.serial.inf.dto.SerialStatusDto;
import com.gzs.learn.serial.inf.exception.SerialException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhisong.guan
 */
@RestController
@RequestMapping("/system/serial")
@RequiredArgsConstructor
public class SerialController extends BaseController {
    private final ISerialClient serialClient;
    private final ISerialQueryClient serialQueryClient;

    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('system:serial:list')")
    public TableDataInfo list(SegmentSearchDto segmentSearchDto) {
        PageResponseDto<SerialAllocDto> pageResponseDto = serialQueryClient.searchList(segmentSearchDto);
        return getDataTable(pageResponseDto);
    }

    @GetMapping(value = "/get_status")
    @PreAuthorize("@ss.hasPermi('system:serial:list')")
    public AjaxResult getStatus() {
        BaseResponseDto<List<SerialStatusDto>> baseResponseDto = serialQueryClient.getSerialStatus();
        return AjaxResult.success(baseResponseDto);
    }

    @GetMapping(value = "/get")
    @PreAuthorize("@ss.hasPermi('system:serial:list')")
    public AjaxResult getSegment(String bizKey) {
        BaseResponseDto<SerialAllocDto> response = serialQueryClient.getSerialInfoByKey(bizKey);
        return AjaxResult.success(response);
    }

    @PostMapping(value = "/add")
    @PreAuthorize("@ss.hasPermi('system:serial:update')")
    public AjaxResult addSegment(SerialAllocDto serialAllocDto) {
        BaseResponseDto<Boolean> baseResponseDto = serialClient.addSegment(serialAllocDto);
        return AjaxResult.success(baseResponseDto);
    }

    @PostMapping(value = "/update")
    @PreAuthorize("@ss.hasPermi('system:serial:update')")
    public AjaxResult updateSegment(SerialAllocDto serialAllocDto) {
        BaseResponseDto<Boolean> baseResponseDto = serialClient.updateSegment(serialAllocDto);
        return AjaxResult.success(baseResponseDto);
    }

    @PostMapping(value = "/update_status")
    @PreAuthorize("@ss.hasPermi('system:serial:update')")
    public AjaxResult updateStatus(String key, Integer status) {
        if (status == null || StringUtils.isBlank(key)) {
            throw new SerialException(String.format("params %s,%s is not illegal", key, status));
        }
        SerialAllocDto serialAllocDto = new SerialAllocDto();
        serialAllocDto.setKey(key);
        serialAllocDto.setStatus(status);
        BaseResponseDto<Boolean> baseResponseDto = serialClient.updateSegment(serialAllocDto);
        return AjaxResult.success(baseResponseDto);
    }

}
